//============================================================================
// Copyright (C) 2010 Brett R. Jones
// All Rights Reserved
//
// You may redistribute and/or modify for non commercial and commercial uses
// provided this copyright notice remains in place and is not modified
//
// This code is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================
#include "DtwGame.h"
//------------------------------------------------------------------------------
//! average the orientation y axis value so world tilt is less jittery
float DtwStatePlay::GetYAxisAverage( float f32RotY )
{
	return f32RotY;
	/*
	m_af32YAxisOrientationAvg[ m_iYAxisAvgIdx ] = f32RotY;
	m_iYAxisAvgIdx++;
	if( m_iYAxisAvgIdx >= Y_AXIS_ORIENTATION_MAX_SAMPLES )
	{
		m_iYAxisAvgIdx = 0;
	}
	F32 f32Avg = 0;
	for( int i = 0; i < Y_AXIS_ORIENTATION_MAX_SAMPLES; i++ )
	{
		f32Avg += m_af32YAxisOrientationAvg[ i ];
	}
	return f32Avg/Y_AXIS_ORIENTATION_MAX_SAMPLES;
	*/

}
//------------------------------------------------------------------------------
//! handle orientation events
bool DtwStatePlay::OnOrientationEvent( F32 f32RotX, F32 f32RotY, F32 f32RotZ  )
{
	if( false == m_bRunning) return 0; // wait till game started
	//log_msg( 0, "Orientation Event pos %3.2f %3.2f %3.2f", f32RotX, f32RotY, f32RotZ);
	if( m_poEntEarth && m_poEntEarth->m_poSceneNode )
	{
		irr::core::vector3df v3fRot = m_poEntEarth->m_poSceneNode->getRotation();
		v3fRot.Z = (GetYAxisAverage( f32RotY) * 1.5f);
		m_poEntEarth->m_poSceneNode->setRotation(v3fRot);
	}
	return 0;
}
//------------------------------------------------------------------------------
//! handle mouse events
bool DtwStatePlay::OnMouseEvent( EMouseButtonType eMouseButType, EMouseEventType eMouseEventType, int iMouseXPos, int iMouseYPos  )
{
	//log_msg( 0, "DtwStatePlay::OnMouseEvent: eMouseButType %d, eMouseEventType %d, X=%d, Y=%d\n", eMouseButType, eMouseEventType, iMouseXPos, iMouseYPos );
	switch( eMouseEventType )
	{
	case eMouseEventDown:
		if( eMouseButtonLeft != eMouseButType )
		{
			// only handle left button
			return false;
		}
		// Remember our initial down event location.
		m_u32TouchStartMs = g_u32ElapsedTimeMs;
		m_f32TouchDnX = (F32)iMouseXPos;
		m_f32TouchDnY = (F32)iMouseYPos;
		m_bTouchHasNotMoved = true;
		m_poTouchSceneNode = NULL;
		if( m_poCollideMgr )
		{
			//log_msg( 0, "Touch action down: have collision mgr\n");
			// get scene node touched if any
			irr::core::position2d<irr::s32> pos((irr::s32)iMouseXPos, (irr::s32)iMouseYPos);
			m_poTouchSceneNode = TouchedSceneNode( pos, 10, 1 );
			if( m_poTouchSceneNode )
			{
				//log_msg( 0, "Touch action down: node touched %d\n", m_poTouchSceneNode );
				m_u32TouchStartMs = g_u32ElapsedTimeMs;
				// set velocity
				//+Z move left
				//-Z move right
				//+X move up
				//-X move down

				//log_msg( 0, "Touched node %d pos %d %d\n", m_poTouchSceneNode, iMouseXPos, iMouseYPos);
				//log_msg( 0, "comet red %d particles %d comet blue %d  particles %d progress bar %d asteroid red node %d asteroid blue node %d \n", 
				//	m_poEntCometRed->m_poSceneNode, 
				//	m_poEntCometRed->m_poParticles, 
				//	m_poEntCometBlue->m_poSceneNode, 
				//	m_poEntCometBlue->m_poParticles, 
				//	m_poProgressBar,
				//	m_poEntAsteroidRed,
				//	m_poEntAsteroidBlue
				//	);

				if( m_poEntCometRed->IsMySceneNode( m_poTouchSceneNode ))
				{
					log_msg( 0, "Playing fireball\n");
					g_oSndMgr.SfxPlay( "fireball.ogg" );
				}
				else if( m_poEntCometBlue->IsMySceneNode( m_poTouchSceneNode ))
				{
					log_msg( 0, "Playing fireball\n");
					g_oSndMgr.SfxPlay( "fireball.ogg" );
				}
				else
				{
					m_poTouchSceneNode = 0;
				}
				//log_msg( 0, "Touch action complete\n");
			}
			else
			{
				//log_msg( 0, "Touch action down: no node touched\n");
			}
		}

		break;

	case eMouseEventMove:
		 if( m_bTouchHasNotMoved )
		 {
			  float x = (F32)iMouseXPos;
			  float y = (F32)iMouseYPos;
			  // Have we moved out of the threshold radius of initial touch?
			  int deltaXFromDown = (int) (x - m_f32TouchDnX);
			  int deltaYFromDown = (int) (y - m_f32TouchDnY);
			  int distance = (deltaXFromDown * deltaXFromDown)
						+ (deltaYFromDown * deltaYFromDown);
			  if (distance > m_f32ScaledTouchSlopSquared)
			  {
				   // moved too far so cancel long-press.
				  m_bTouchHasNotMoved = false;
			  }
		 }
		 break;

	case eMouseEventUp:
		if( eMouseButtonLeft != eMouseButType )
		{
			// only handle left button
			return false;
		}
		//log_msg( 0, "DtwStatePlay::OnMouseEvent: eMouseEventUp, X=%d, Y=%d\n", iMouseXPos, iMouseYPos );
		m_f32TouchUpX = (F32)iMouseXPos;
		m_f32TouchUpY = (F32)iMouseYPos;
		if( m_poTouchSceneNode )
		{
			GameObjToss();
		}

		m_bIsLongPress = false;
		m_bTouchHasNotMoved = true;
		break;

	default:
		break;
	}
	return false;
}
